<template>
	<view>
		<view class="navbar" style="padding: 20rpx;height: 180rpx;background-color: #fff;">
			<view class="title" style="text-align: center;color: #dd6161;font-size: 45rpx;font-weight: bold;">首页</view>
			<view class="search" style="width: 95%;height: 64rpx;margin: 20rpx auto;flex-shrink: 0;display: flex;
			flex-direction: row;justify-content: start;align-items: center;background-color: #f2f2f2;border: 2rpx solid #dd6161;
			border-radius: 50rpx;position: relative;">
				<u-icon name="search" style="margin-left: 20rpx;"></u-icon>
				<input type="text" placeholder="请输入关键字" style="margin-left: 20rpx;" v-model="value">
				<button @click="submit" style="width: 120rpx;height: 95%;color: white;background-color: #fa3534;font-size: 28rpx;
				display: flex;justify-content: center;align-items: center;position: absolute;right: 1rpx;top: 1rpx;border-radius: 50rpx;">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y="true" class="main">
			<view class="content">
				<view class="swiper" style="padding: 20rpx;background-color: #fff;">
					<u-swiper :list="swiperList" @click="to" height="300"></u-swiper>
				</view>
				<view class="cate">
					<view class="cate-item" v-for="item in cateList">
						<image :src="$baseUrl + item.imgUrl" mode=""></image>
						<text>{{item.serviceName}}</text>
					</view>
					<view class="cate-item">
						<u-icon name="list-dot" size="80" class="icon" color="#2979ff"></u-icon>
						<text>更多服务</text>
					</view>
				</view>
				<view class="hot">
					<view class="title">热门新闻</view>
					<view class="hot-list">
						<navigator class="hot-item" v-for="item in hotList" :url="`/pages/news-detail/news-detail?id=${item.id}`">
							<image :src="$baseUrl + item.cover" mode=""></image>
							<text>{{item.title}}</text>
						</navigator>
					</view>
				</view>
				<view class="news">
					<u-sticky offset-top="90">
						<u-tabs :list="tabList" :is-scroll="true" :current="tabCurrent" @change="tabChange"></u-tabs>
					</u-sticky>
					<view class="news-list">
						<view class="news-item" v-for="item in selectList">
							<view class="left">
								<view class="top">
									<text class="name">{{item.title}}</text>
									<rich-text :nodes="item.content" class="desc"></rich-text>
								</view>
								<view class="bottom">
									<text>{{item.commentNum}} 评论</text>
									<text>{{item.publishDate}}</text>
								</view>
							</view>
							<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`" class="right">
								<image :src="$baseUrl + item.cover" mode=""></image>
							</navigator>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList: [],
				cateList: [],
				hotList:[],
				tabList: [],
				tabCurrent: 0,
				selectList: [],
				value: ''
			}
		},
		onLoad() {
			this.getSwiper()
			this.getCate()
			this.getHot()
			this.getTab()
		},
		methods: {
			getSwiper(){
				this.$request({
					url: '/prod-api/api/rotation/list',
					data:{
						type: '2'
					}
				}).then(res => {
					console.log(res)
					this.swiperList = res.rows.map(item => ({image: this.$baseUrl + item.advImg, id: item.targetId}))
				})
			},
			getCate(){
				this.$request({
					url: '/prod-api/api/service/list'
				}).then(res => {
					console.log(res)
					this.cateList = res.rows.splice(0,9)
				})
			},
			getHot(){
				this.$request({
					url: '/prod-api/press/press/list',
					data: {
						hot: 'Y'
					}
				}).then(res => {
					console.log(res)
					this.hotList = res.rows
				})
			},
			tabChange(index){
				this.tabCurrent = index
				this.getSelect()
			},
			getTab(){
				this.$request({
					url: '/prod-api/press/category/list'
				}).then(res => {
					console.log(res)
					this.tabList = res.data.map(item => ({name: item.name, type: item.id}))
					this.getSelect()
				})
			},
			getSelect(){
				this.$request({
					url: '/prod-api/press/press/list',
					data: {
						type: this.tabList[this.tabCurrent].type
					}
				}).then(res => {
					console.log(res)
					this.selectList = res.rows
				})
			},
			to(index){
				uni.navigateTo({
					url:`/pages/news-detail/news-detail?id=${this.swiperList[index].id}`
				})
			},
			submit(){
				uni.navigateTo({
					url:'/pages/search/search?key=' + this.value
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		height: 100%;
		view{
			height: 100%;
			display: flex;
			flex-direction: column;
			.main{
				flex: 1;
				overflow: hidden;
				.content{
					height: fit-content;
					background-color: #f7f7f7;
					.cate{
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						justify-content: space-between;
						align-items: center;
						padding: 20rpx;
						.cate-item{
							padding: 20rpx;
							display: flex;
							flex-direction: column;
							justify-content: center;
							align-items: center;
							width: 120rpx;
							height: fit-content;
							image{
								width: 100rpx;
								height: 100rpx;
							}
							text{
								width: 120rpx;
								white-space: nowrap;
								text-overflow: ellipsis;
								overflow: hidden;
								font-size: 26rpx;
								color: #666;
								margin-top: 10rpx;
								text-align: center;
							}
							.icon{
								width: 100rpx;
								height: 100rpx;
								border-radius: 50%;
								border: 4rpx solid #2979ff;
								display: flex;
								justify-content: center;
								align-items: center;
							}
						}
					}
					.hot{
						padding: 20rpx;
						background-color: #fff;
						.title{
							font-size: 45rpx;
							color: #dd6161;
							font-weight: 400;
							padding: 20rpx;
						}
						.hot-list{
							padding: 20rpx 0;
							border-top: 2rpx solid #e4e7ed;
							display: flex;
							flex-direction: row;
							justify-content: space-between;
							.hot-item{
								flex: 1;
								display: flex;
								flex-direction: column;
								justify-content: center;
								align-items: center;
								image{
									width: 300rpx;
									height: 250rpx;
									border-radius: 20rpx;
								}
								text{
									font-size: 26rpx;
									color: #333;
									width: 300rpx;
									display: -webkit-box;
									-webkit-box-orient: vertical;
									-webkit-line-clamp: 2;
									overflow: hidden;
									line-height: 1.5;
									margin-top: 20rpx;
								}
							}
						}
					}
					.news{
						margin-top: 20rpx;
						background-color: #fff;
						.news-list{
							display: flex;
							flex-direction: column;
							padding: 20rpx;
							.news-item{
								padding: 20rpx 0;
								display: flex;
								flex-direction: row;
								justify-content: space-between;
								border-top: 2rpx solid #e4e7ed;
								.left{
									.top{
										width: 450rpx;
										height: 168rpx;
										.name{
											font-size: 32rpx;
											line-height: 1.5;
											font-weight: bold;
											display: -webkit-box;
											-webkit-box-orient: vertical;
											-webkit-line-clamp: 2;
											overflow: hidden;
										}
										.desc{
											font-size: 26rpx;
											line-height: 1.5;
											display: -webkit-box;
											-webkit-box-orient: vertical;
											-webkit-line-clamp: 2;
											overflow: hidden;
										}
									}
									.bottom{
										display: flex;
										flex-direction: row;
										justify-content: start;
										text{
											margin-left: 20rpx;
											color: #909399;
											font-size: 22rpx;
											&:first-child{
												margin-left: 0;
											}
										}
									}
								}
								.right{
									display: flex;
									justify-content: center;
									align-items: center;
									image{
										width: 250rpx;
										height: 200rpx;
									}
								}
							}
						}
					}
				}
			}
		}
	}
</style>
